/*
    Speedbump aka Project Kimbula
    Copyright (C) 2010  Ping Shin Ching

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses.

	Ping Shin Ching
	righteous.ninja@gmail.com OR ping_sc@hotmail.com
	More information at http://www.tidytutorials.com/p/speedbump.html
 */

#ifndef httphandler_h
#define httphandler_h

#include "xsocket.h"
#include "headerhandler.h"
//#include "defaultcontenthandler.h"
#include "ratehandler.h"
#include "contenthandlerinterface.h"
#include "handlerresponses.h"
#include "capinterface.h"

#define MAX_HANDLER_BUFFER_SIZE 8192 
#define HANDLER_TICKS 10


#define MAX_HANDLER_ZLIB_BFFER_SIZE 4194304
#define HANDLER_ZLIB_MULTIPLIER  4 


class ProtocolHTTPHandler;
class HeaderHandler;

class HTTPHandler{
private:
	char* host_address;
	int	host_port;
	int name;
	char* redirecturl;
	//char remoteaddr[SP_SOCKADDRESS_LENGTH];
	ipaddress remoteaddr;
	unsigned int ratelimit;
	int rateadjustment;
	char* buffer;
	unsigned int buffersize;
	char bufferpos;
	int header_finished;
	int procid;
	int connectmethod;
	unsigned int processedbytes;
	int handlerticks[HANDLER_TICKS];
	short dirtybit;

	short fullforward;
	short cascadeon;

	void* param1;
	void** capi_param1;

	//unsigned int lastupdatedtime;
	RateHandler ratehandler;
	HeaderHandler	iheaderd;

	ContentHandlerInterface* chitf;

	void cleanupBuffers();
	void getIntermediateRequest();


public:
	HTTPHandler(int);
	HANDLER_RESPONSE HandleData(char*, int*);
	HANDLER_RESPONSE HandleDownstreamData(char*, int*);
	HANDLER_RESPONSE GetHandledData(char*, int*);
	HANDLER_RESPONSE Connect(char**);
	//HANDLER_RESPONSE GetHandledDownstreamData(HandlerData*, char*, int*);
	HANDLER_RESPONSE CheckHandlerData();

	void ResetHeaderCapture();
	void CleanupHandlerData();
	void KillBuffer();
	const char* GetConnectionInfo();

	//void PrepareHandlerData(HandlerData* hd,const char* id);
	int ModifyHeaders(HeaderHandler*);
	void FillHostAndPort(HeaderHandler*);
	friend class RateHandler;

	friend class ProtocolHTTPHandler;

};

#endif
